package wwl.lsf.hellospringboot.test.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import org.junit.Test;

public class channel {

  @Test
  public void testWrite() throws Exception {

    String str = "写文件测试";
    FileOutputStream fout = new FileOutputStream("复制.txt");

    FileChannel fc = fout.getChannel();
    ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
    byteBuffer.put(str.getBytes());
    byteBuffer.flip();
    fc.write(byteBuffer);
    fout.close();
  }

  @Test
  public void testRead() throws Exception {

    File file = new File("复制.txt");

    FileInputStream fin = new FileInputStream(file);

    FileChannel fc = fin.getChannel();
    ByteBuffer allocate = ByteBuffer.allocate((int) file.length());
    int read = fc.read(allocate);
    System.out.println(new String(allocate.array()));
    fin.close();
  }

  @Test
  public void testCopy() throws Exception {

    FileInputStream inputStream = new FileInputStream("E:\\BaiduNetdiskDownload\\阶段7调优\\06-NIO与Netty编程\\05 自定义RPC\\49.mp4");
    FileOutputStream outputStream = new FileOutputStream("E:\\BaiduNetdiskDownload\\阶段7调优\\06-NIO与Netty编程\\05 自定义RPC\\94.mp4");
    FileChannel inc = inputStream.getChannel();
    FileChannel outputStreamChannel = outputStream.getChannel();

    inc.transferTo(0, inc.size(), outputStreamChannel);
    inc.close();
    outputStreamChannel.close();


  }
}
